<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!--[if lte IE 9]><script>(function(f){window.setTimeout=f(window.setTimeout);window.setInterval=f(window.setInterval)})(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c.apply(this,a)},t)}});</script><![endif]-->
<!--[if IE 8]><script src="https://cdn.bootcss.com/ie8/0.8.0/ie8.js"></script><![endif]-->
<script src="https://cdn.bootcss.com/dom4/1.8.5/dom4.max.js"></script>
<!--[if IE 8]>
<script src="assets/js/dre-ie8-upfront-fix.max.js"></script>
<![endif]-->
<script src="https://cdn.bootcss.com/document-register-element/1.7.0/document-register-element.js"></script>
<!--[if IE 8]>
<script src="https://cdn.bootcss.com/es5-shim/4.5.10/es5-shim.js"></script>
<script src="https://cdn.bootcss.com/es5-shim/4.5.10/es5-sham.js"></script>
<![endif]-->
<script>
    if (!window.Proxy) {
        var head = document.head || document.getElementsByTagName('head')[0];
        var script1 = document.createElement("script");
        script1.src = "https://cdn.bootcss.com/es6-shim/0.35.3/es6-shim.min.js";
        head.appendChild(script1);
    }

    if (!Object.entries) {
        Object.entries = function( obj ){
            var ownProps = Object.keys( obj ),
                i = ownProps.length,
                resArray = new Array(i); // preallocate the Array
            while (i--)
                resArray[i] = [ownProps[i], obj[ownProps[i]]];

            return resArray;
        };
    }
</script>
<script>
    ;(function(constructor) {
        // childElementCount
        if (constructor &&
            constructor.prototype &&
            constructor.prototype.childElementCount == null) {
            Object.defineProperty(constructor.prototype, 'childElementCount', {
                get: function() {
                    var i = 0, count = 0, node, nodes = this.childNodes;
                    while (node = nodes[i++]) {
                        if (node.nodeType === 1) count++;
                    }
                    return count;
                }
            });
        }

        // firstElementChild
        if (constructor &&
            constructor.prototype &&
            constructor.prototype.firstElementChild == null) {
            Object.defineProperty(constructor.prototype, 'firstElementChild', {
                get: function() {
                    var node, nodes = this.childNodes, i = 0;
                    while (node = nodes[i++]) {
                        if (node.nodeType === 1) {
                            return node;
                        }
                    }
                    return null;
                }
            });
        }

        // lastElementChild
        if (constructor &&
            constructor.prototype &&
            constructor.prototype.lastElementChild == null) {
            Object.defineProperty(constructor.prototype, 'lastElementChild', {
                get: function() {
                    var node, nodes = this.childNodes, i = nodes.length - 1;
                    while (node = nodes[i--]) {
                        if (node.nodeType === 1) {
                            return node;
                        }
                    }
                    return null;
                }
            });
        }
    })(window.Node || window.Element);
</script>
<script>
window.defineCustomElement = function (name, defines) {
    for (var key in defines) {
        if (defines[key] && {}.toString.call(defines[key]) === '[object Function]') {
            defines[key] = {value: defines[key]}
        }
    }
    document.registerElement(
        name,
        {
            prototype: Object.create(
                HTMLElement.prototype, defines
            )
        }
    );
};
</script>

    <link rel="stylesheet" href="../../ui.css?v=1763651972">
    <script src="../../ui.js?v=1584926271"></script>

    <script src="../../../src/aux-text/index.js?v=17612523"></script>
    <link rel="stylesheet" href="../../../src/aux-text/index.css?v=344362009">

    <style>
    </style>
    
    <link rel="stylesheet" href="./index.css?v=700452235">
    <style>
        auc-list-menu {
            width: 230px;
        }
    </style>

</head>
<body>

    <div>      
        <h3>默认</h3> 
        <auc-list-menu id="menu" class="open">
            <auc-item id="a">item 1</auc-item>
            <auc-item>
                item 2
                <auc-list-menu> 
                    <auc-item>item 2.1</auc-item>
                </auc-list-menu>      
            </auc-item>                        
            <auc-gutter></auc-gutter>
            <auc-item>item 3</auc-item>
        </auc-list-menu>              
    </div>
    <script>

function hover(el, onOver, onOut) {
  var x, y, pX, pY;
  var h = {},
    state = 0,
    timer = 0;

  var options = {
    sensitivity: 7,
    interval: 100,
    timeout: 0
  };

  function delay(el, e) {
    if (timer) timer = clearTimeout(timer);
    state = 0;
    return onOut.call(el, e);
  }

  function tracker(e) {
    x = e.clientX;
    y = e.clientY;
  }

  function compare(el, e) {
    if (timer) timer = clearTimeout(timer);
    if ((Math.abs(pX - x) + Math.abs(pY - y)) < options.sensitivity) {
      state = 1;
      return onOver.call(el, e);
    } else {
      pX = x;
      pY = y;
      timer = setTimeout(function() {
        compare(el, e);
      }, options.interval);
    }
  }

  // Public methods
  h.options = function(opt) {
    options = Object.assign({}, options, opt);
    return h;
  };

  function dispatchOver(e) {
    if (timer) timer = clearTimeout(timer);
    el.removeEventListener('mousemove', tracker, false);

    if (state !== 1) {
      pX = e.clientX;
      pY = e.clientY;

      el.addEventListener('mousemove', tracker, false);

      timer = setTimeout(function() {
        compare(el, e);
      }, options.interval);
    }

    return this;
  }

  function dispatchOut(e) {
    if (timer) timer = clearTimeout(timer);
    el.removeEventListener('mousemove', tracker, false);

    if (state === 1) {
      timer = setTimeout(function() {
        delay(el, e);
      }, options.timeout);
    }

    return this;
  }

  h.remove = function() {
    if (!el) return;
    el.removeEventListener('mouseover', dispatchOver, false);
    el.removeEventListener('mouseout', dispatchOut, false);
  };

  if (el) {
    el.addEventListener('mouseover', dispatchOver, false);
    el.addEventListener('mouseout', dispatchOut, false);
  }

  return h;
};            

            let currentShowSubMenu = null;
            
            function liveCheck(el, s, cb) {
                function done() {
                    el.removeEventListener("mouseout", self);
                    s.removeEventListener("mouseover", toTargetIn);
                    s.removeEventListener("mouseout", toTargetOf);                            
                    el.__lock = null;
                }                
                function toTargetIn(e) {
                     el.__totarget = true;
                }
                function toTargetOf(e) {
                    el.__totarget = false;                                          
                    cb(e, done);
                }
                if (!el.__lock) {
                    el.__totarget = false;
                    el.addEventListener("mouseout", function(e) {
                        var self = this;     
                        if (!el.__lock) {
                            setTimeout(function() {
                               if (!el.__totarget) {
                                    cb(e, done);
                               }                            
                            }, 170);                            
                            el.__lock = true;
                        }
                    });
                    s.addEventListener("mouseover", toTargetIn);
                    s.addEventListener("mouseout", toTargetOf);
                }
            }

            function onItem(e, menu, idnex) {
                // if ( document.querySelector("#menu").auiIsChildren(e.target)){
                    if (currentShowSubMenu) {                
                        currentShowSubMenu.auiRemoveOnceClass("open")
                        currentShowSubMenu = null;
                        // console.log("remove");
                    }

                    if (!menu.___statslock) {
                        menu.___statslock = true;
                        function check(e) {
                            return function() {
                                if (menu.___statslock) {
                                    var s = menu.auiFindSelector("> auc-list-menu");
                                    if (s) {
                                        currentShowSubMenu = s;
                                        if (!s.classList.contains("open")) {
                                            s.auiAddOnceClass("open")
                                            liveCheck(menu, s, function(mouseoutevent, done) {
                                                if (s) {                                
                                                    s.auiRemoveOnceClass("open");
                                                    done();
                                                } else {
                                                    done();
                                                }
                                            });
                                        }
                                    } 
                                }
                            }
                        }
                        let n = check(e);
                        setTimeout(function() {
                            n();
                        }, 30);
                    }
               // }
            }
            

            function createSubmenuDetect(el, selector) {
                var menus = [];
                var ret = {};
                ret.remove = function(cb) {
                    menus.forEach(function(menu) {
                        menu.$hovercontrol.remove();
                        menu.$hovercontrol = null;
                    });
                    if (cb) {
                        cb();
                    }
                }
                ret.reset = function() {
                    ret.remove(function() {
                        ret.init();
                    });
                }
                ret.init = function() {
                    menus = Array.prototype.slice.call(el.auiFindSelectorAll(selector));
                    menus.forEach(function(menu, index) {
                        menu.$hovercontrol = hover(menu, function(e) {
                            onItem(e, menu, index);
                        }, function(e) {
                            menu.___statslock = false;
                        });
                    });
                }
                return ret;
            }

         var menu = document.querySelector("#menu");
            menu.$control = createSubmenuDetect(menu, "> auc-item");

            

    </script>

</body>
</html>
